package com.chen.controller;

import com.chen.aop.OperLog;
import com.chen.dto.PageData;
import com.chen.dto.ResponData;
import com.chen.entity.Fan;
import com.chen.entity.FansTestingTime;
import com.chen.general.Constans;
import com.chen.general.ModuleType;
import com.chen.general.StateCode;
import com.chen.service.FanService;
import com.chen.service.FansTestingTimeService;
import com.chen.until.MyUntils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("api/fan")
public class FanController {

    @Autowired
    private FanService fanService;

    @Autowired
    protected FansTestingTimeService fansTestingTimeService;


    @OperLog(operModul = ModuleType.FAN_MODAL, operType = ModuleType.ADD, operDesc = "添加风机")
    @RequestMapping("insertFans")
    public ResponData insertFans(
            @RequestParam(name = "uid") String uid,
            @RequestParam(name = "uname") String uname,
            @RequestParam(name = "fcode") String fcode,
            @RequestParam(name = "attrProjectId") String attrProjectId,
            @RequestParam(defaultValue = "", required = false, name = "bwsj") String bwsj,
            @RequestParam(defaultValue = "", required = false, name = "sjsj") String sjsj,
            @RequestParam(defaultValue = "", required = false, name = "qnjsj") String qnjsj,
            @RequestParam(defaultValue = "", required = false, name = "bnjsj") String bnjsj,
            @RequestParam(defaultValue = "", required = false, name = "xjsj") String xjsj,
            @RequestParam(defaultValue = "", required = false, name = "xcqnjsj") String xcqnjsj,
            @RequestParam(defaultValue = "", required = false, name = "xcbnjsj") String xcbnjsj,
            @RequestParam(defaultValue = "", required = false, name = "xcxjsj") String xcxjsj,
            @RequestParam(defaultValue = "", required = false, name = "remark") String remark
    ) {

        int i = this.fanService.queryFanNumByFcode(fcode, attrProjectId);
        if (i > 0) {
            return new ResponData(Constans.REPEAT_ADD, StateCode.ERROR_CODE);
        }

        String isConNet;
        if (!bwsj.equals("")) {
            isConNet = "1";
        } else {
            isConNet = "0";
        }
        Fan fan = new Fan()
                .setId(MyUntils.returnId())
                .setFcode(fcode)
                .setCreate_time(MyUntils.returnTime())
                .setState("1")
                .setCreate_user_name(uname)
                .setCreate_user_id(uid)
                .setAttr_project_id(attrProjectId)
                .setIs_con_net(isConNet)
                .setRemark(remark);

        FansTestingTime fansTestingTime = new FansTestingTime()
                .setId(MyUntils.returnId())
                .setCreate_time(MyUntils.returnTime())
                .setBnjsj(MyUntils.formartTime(bnjsj))
                .setBwsj(MyUntils.formartTime(bwsj))
                .setSjsj(MyUntils.formartTime(sjsj))
                .setQnjsj(MyUntils.formartTime(qnjsj))
                .setXjsj(MyUntils.formartTime(xjsj))
                .setFanid(fan.getId())
                .setCreate_user_id(uid)
                .setYjxcqnjsj(MyUntils.formartTime(xcqnjsj))
                .setYjxcbnjsj(MyUntils.formartTime(xcbnjsj))
                .setYjxcxjsj(MyUntils.formartTime(xcxjsj));
        boolean b = this.fanService.insertFan(fan);
        boolean c = this.fansTestingTimeService.insertFansTestTime(fansTestingTime);
        if (b && c) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }
    }

    @OperLog(operModul = ModuleType.FAN_MODAL, operType = ModuleType.UPDATE, operDesc = "修改风机")
    @RequestMapping("updateFans")
    public ResponData UpdateFans(
            @RequestParam(name = "uid") String uid,
            @RequestParam(name = "uname") String uname,
            @RequestParam(name = "fcode") String fcode,
            @RequestParam(name = "attrProjectId") String attrProjectId,
            @RequestParam(defaultValue = "", required = false, name = "bwsj") String bwsj,
            @RequestParam(defaultValue = "", required = false, name = "sjsj") String sjsj,
            @RequestParam(defaultValue = "", required = false, name = "qnjsj") String qnjsj,
            @RequestParam(defaultValue = "", required = false, name = "bnjsj") String bnjsj,
            @RequestParam(defaultValue = "", required = false, name = "xjsj") String xjsj,
            @RequestParam(defaultValue = "", required = false, name = "xcqnjsj") String xcqnjsj,
            @RequestParam(defaultValue = "", required = false, name = "xcbnjsj") String xcbnjsj,
            @RequestParam(defaultValue = "", required = false, name = "xcxjsj") String xcxjsj,
            @RequestParam(defaultValue = "", required = false, name = "remark") String remark,
            @RequestParam(name = "fid") String fid,
            @RequestParam(name = "ftid") String ftid) {
        String isConNet;
        if (!bwsj.equals("")) {
            isConNet = "1";
        } else {
            isConNet = "0";
        }
        Fan fan = new Fan()
                .setId(fid)
                .setFcode(fcode)
                .setState("1")
                .setCreate_user_name(uname)
                .setCreate_user_id(uid)
                .setAttr_project_id(attrProjectId)
                .setIs_con_net(isConNet)
                .setRemark(remark);

        FansTestingTime fansTestingTime = new FansTestingTime()
                .setId(ftid)
                .setBnjsj(MyUntils.formartTime(bnjsj))
                .setBwsj(MyUntils.formartTime(bwsj))
                .setSjsj(MyUntils.formartTime(sjsj))
                .setQnjsj(MyUntils.formartTime(qnjsj))
                .setXjsj(MyUntils.formartTime(xjsj))
                .setFanid(fan.getId())
                .setCreate_user_id(uid)
                .setYjxcqnjsj(MyUntils.formartTime(xcqnjsj))
                .setYjxcbnjsj(MyUntils.formartTime(xcbnjsj))
                .setYjxcxjsj(MyUntils.formartTime(xcxjsj));
        boolean b = this.fanService.updateFans(fan);
        boolean c = this.fansTestingTimeService.updateFansTestTime(fansTestingTime);
        if (b && c) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }
    }


    @OperLog(operModul = ModuleType.FAN_MODAL, operType = ModuleType.QUERY, operDesc = "查询风机")
    @RequestMapping("queryFans")
    public ResponData queryFans(
            @RequestParam("currentPage") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam("uid") String uid,
            @RequestParam(name = "fjbh", required = false) String fjbh,
            @RequestParam(name = "projectid", required = false) String projectid,
            @RequestParam(name = "btime", required = false) String btime,
            @RequestParam(name = "etime", required = false) String etime) {

        if (btime != null && btime != "") {
            btime = btime.replace("-", "");
        }
        if (etime != null && etime != "") {
            etime = etime.replace("-", "");
        }

        PageData pageData = this.fanService.queryFans(pageNum, pageSize, fjbh, projectid, btime, etime, uid);
        return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE, pageData);
    }

    @OperLog(operModul = ModuleType.FAN_MODAL, operType = ModuleType.UPDATE, operDesc = "修改风机状态")
    @RequestMapping("changeFanState")
    public ResponData changeFanState(
            @RequestParam(name = "fanid") String fansid,
            @RequestParam(name = "state") String state
    ) {
        Fan fan = this.fanService.queryFanById(new Fan().setId(fansid));
        fan.setState(state);
        boolean b = this.fanService.updateFans(fan);
        if (b) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        } else {
            return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
        }

    }


    @RequestMapping("queryFensById")
    public ResponData queryFensById(String fid) {
        HashMap fan = this.fanService.queryFenMessageById(fid);
        return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE, fan);

    }

    @OperLog(operModul = ModuleType.FAN_MODAL, operType = ModuleType.DELETE, operDesc = "删除风机")
    @RequestMapping("deleteFenById")
    public ResponData deleteFenById(String fid) {
        FansTestingTime fansTestingTime = this.fansTestingTimeService.queryFansTestingTimeByFid(fid);
        boolean a = this.fansTestingTimeService.deleteFansTestingTimeById(fansTestingTime);
        boolean b = this.fanService.deleteFenById(fid);
        if (a && b) {
            return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE);
        }
        return new ResponData(Constans.ERROR_MESS, StateCode.ERROR_CODE);
    }


    //查看已逾期项目
    @OperLog(operModul = ModuleType.FAN_TIME_MODAL, operType = ModuleType.QUERY, operDesc = "查看已逾期项目")
    @RequestMapping("yjyqFans")
    public ResponData yjyqFans(
            @RequestParam("currentPage") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(name = "projectid", required = false, defaultValue = "") String projectid,
            @RequestParam(name = "type", required = false, defaultValue = "") String type
    ) {

        String queryStr = "";
        if (!"".equals(type)) {
            String beginStr = " AND (";
            String endStr = " ) ";
            if (type.contains("0")) {
                //要看全年检
                queryStr += " NOT (yuqi_mes.qnjyqsj = '未逾期')  OR";
            }
            if (type.contains("1")) {
                //要看半年检
                queryStr += " NOT (yuqi_mes.bnjyqsj = '未逾期')  OR";
            }
            if (type.contains("2")) {
                //要看巡检
                queryStr += " NOT (yuqi_mes.xjyqsj = '未逾期')  OR";
            }
            queryStr = queryStr.substring(0, queryStr.length() - 2);
            queryStr = beginStr + queryStr + endStr;
        }
        PageData pageData = this.fanService.queryYqFansByPid(pageNum, pageSize, projectid, queryStr);
        return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE, pageData);
    }


    //查看即将逾期项目
    @OperLog(operModul = ModuleType.FAN_TIME_MODAL, operType = ModuleType.QUERY, operDesc = "查看即将逾期项目")
    @RequestMapping("jjyqFans")
    public ResponData jjyqFans(
            @RequestParam("currentPage") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(name = "projectid") String projectid,
            @RequestParam(name = "duration", required = false, defaultValue = "") String duration,
            @RequestParam(name = "type", defaultValue = "", required = false) String type
    ) {



        String queryStr1 = "";
        if (!"".equals(duration)) {
            queryStr1 = "AND ( jijiangyuqi_mes.jqnjdqsj BETWEEN 1 AND " + duration
                    + " OR jijiangyuqi_mes.jbnjdqsj BETWEEN 1 AND " + duration
                    + " OR jijiangyuqi_mes.jxjdqsj BETWEEN 1  AND " + duration
                    +"  OR  SUBSTR( jijiangyuqi_mes.jqnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "   OR  SUBSTR( jijiangyuqi_mes.jbnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "   OR  SUBSTR( jijiangyuqi_mes.jxjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' "
                    + ")";
        }


        String queryStr2 = "";
        if (!"".equals(type) &&  !"".equals(duration)) {
            String beginStr = " AND (";
            String endStr = " OR  SUBSTR( jijiangyuqi_mes.jqnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "  OR  SUBSTR( jijiangyuqi_mes.jbnjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期' " +
                    "  OR  SUBSTR( jijiangyuqi_mes.jxjdqsj,  '1', LOCATE('期', jijiangyuqi_mes.jqnjdqsj)  ) = '逾期'   ) ";
            if (type.contains("0")) {
                //要看全年检
                queryStr2 += "   jijiangyuqi_mes.jqnjdqsj BETWEEN 1 AND " + duration  +  " OR";
            }
            if (type.contains("1")) {
                //要看半年检
                queryStr2 += "  jijiangyuqi_mes.jbnjdqsj BETWEEN 1 AND " + duration +  " OR";
            }
            if (type.contains("2")) {
                //要看巡检
                queryStr2 += " jijiangyuqi_mes.jxjdqsj BETWEEN 1 AND " + duration +  "  OR";
            }
            queryStr2 = queryStr2.substring(0, queryStr2.length() - 2);
            queryStr2 = beginStr + queryStr2 + endStr;

        }

        PageData pageData = this.fanService.queryJjYqFansByPid(pageNum, pageSize, projectid, queryStr1,queryStr2);
        return new ResponData(Constans.SUCCESS_MES, StateCode.SUCCEED_CODE, pageData);
    }

}
